/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogDangKyMonDay.java
 *
 * Created on Feb 23, 2013, 10:58:43 PM
 */
package design;

import Util.ConSQL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Thuyngaytho
 */
public class DialogDangKyMonDay extends javax.swing.JDialog {

    Vector tableTitle1 = new Vector();
    Vector tableTitle2 = new Vector();
    Vector tableRecord1 = new Vector();
    Vector tableRecord2 = new Vector();
    String tenGV;
    String maGV;
    ConSQL conn = new ConSQL();

    /** Creates new form DialogDangKyMonDay */
    public DialogDangKyMonDay(java.awt.Frame parent, boolean modal) throws Exception {
        super(parent, modal);
        initComponents();
         lbMaGV.setText("GV001");        
        //
        tenGV = lbTenGV.getText();
        maGV = lbMaGV.getText();
        //
        btnDangKy.setEnabled(false);
        btnHuy.setEnabled(false);
        //
        LoadMonDangki();
        LoadMonhoc();
    }
public DialogDangKyMonDay(java.awt.Frame parent, boolean modal,String maGV,String tenGV,String matkhau) throws Exception {
        super(parent, modal);
        initComponents();                
        lbMaGV.setText(maGV);       
        lbTenGV.setText(tenGV);
        //
        this.tenGV = tenGV;
        this.maGV = maGV;
        //
        btnDangKy.setEnabled(false);
        btnHuy.setEnabled(false);
        //
        LoadMonDangki();
        LoadMonhoc();
        //
    }
    //
    public void createTitle1(){
        tableTitle1.removeAllElements();
        tableTitle1.add("Danh sách môn học");
    }
    public void createTitle2(){
        tableTitle2.removeAllElements();
        tableTitle2.add("Danh sách môn đăng kí");
    }
    public void LoadMonhoc() throws Exception{
       createTitle1();
       tableRecord1.removeAllElements();       
       Statement st = conn.getCon().createStatement();
       ResultSet rs = st.executeQuery("select tenMH from monHoc");
       while(rs.next()){
           Vector data = new Vector();
           data.add(rs.getString(1));
           tableRecord1.add(data);
       }
       tbMonHoc.setModel(new DefaultTableModel(tableRecord1, tableTitle1));
    }
    //
    public String layTenmon(String mamon) throws Exception{
        Statement st = conn.getCon().createStatement();
        ResultSet rs = st.executeQuery("select tenMH from monHoc where maMH='"+mamon+"'");
        if(rs.next()){
            return rs.getString(1).toString();
        }
        return rs.getString(1).toString();
    }
    //
    public String layMamon(String tenmon) throws Exception{
        Statement st = conn.getCon().createStatement();
        ResultSet rs = st.executeQuery("select maMH from monHoc where tenMH=N'"+tenmon+"'");
        if(rs.next()){
            return rs.getString(1).toString();
        }
        return rs.getString(1).toString();
    }
    //
    public void LoadMonDangki() throws Exception{
        createTitle2();
       tableRecord2.removeAllElements();       
       Statement st = conn.getCon().createStatement();
       ResultSet rs = st.executeQuery("select maMH from giaoVien_MH where maGV='"+maGV+"'");
       while(rs.next()){
           String tenmon =layTenmon(rs.getString(1).toString());
           Vector data = new Vector();
           data.add(tenmon);
           tableRecord2.add(data);                                      
       }
       tbMonDK.setModel(new DefaultTableModel(tableRecord2, tableTitle2));
    }
    //
    public String CheckDangki() throws SQLException, Exception{
        String c="";
        int Dongchon = tbMonHoc.getSelectedRow();
        Vector vDongchon = (Vector) tableRecord1.get(Dongchon);
        String tenmon = vDongchon.get(0).toString();
        String mamon = layMamon(tenmon);
        Statement st = conn.getCon().createStatement();
        ResultSet rs = st.executeQuery("select maMH from giaoVien_MH where maMH='"+mamon+"' and maGV='"+maGV+"'");
        if(rs.next()){
            c = "Môn này bạn đã đăng kí!";
        }
        return c;
    }
    //
    public void Dangki() throws Exception{
        int Dongchon = tbMonHoc.getSelectedRow();
        Vector vDongchon = (Vector) tableRecord1.get(Dongchon);
        String tenmon = vDongchon.get(0).toString();
        String mamon = layMamon(tenmon);
        if(JOptionPane.showConfirmDialog(rootPane, "Bạn có muốn đăng kí môn dạy có tên: "+tenmon, "Lựa chọn", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
            if(CheckDangki().equals("")){
            Statement st = conn.getCon().createStatement();
            int rs = st.executeUpdate("insert into giaoVien_MH values('"+maGV+"','"+mamon+"');");
            JOptionPane.showMessageDialog(rootPane, "Đăng kí thành công!");
            }
            else JOptionPane.showMessageDialog(rootPane, CheckDangki());
        }        
    }
    public void Huy() throws Exception{
        int Dongchon = tbMonDK.getSelectedRow();
        Vector vDongchon = (Vector) tableRecord2.get(Dongchon);
        String tenmon = vDongchon.get(0).toString();
        String mamon = layMamon(tenmon);
        if(JOptionPane.showConfirmDialog(rootPane, "Bạn có muốn hủy môn dạy có tên: "+tenmon, "Lựa chọn", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
            Statement st = conn.getCon().createStatement();
            int rs = st.executeUpdate("delete from giaoVien_MH where maMH='"+mamon+"' and maGV = '"+maGV+"'");
            JOptionPane.showMessageDialog(rootPane, "Hủy thành công");
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        lbMaGV = new javax.swing.JLabel();
        lbTenGV = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbMonHoc = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        tbMonDK = new javax.swing.JTable();
        btnDangKy = new javax.swing.JButton();
        btnLamMoi = new javax.swing.JButton();
        btnHuy = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin giáo viên", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 18))); // NOI18N

        jLabel1.setText("Mã giáo viên :");

        jLabel2.setText("Tên giáo viên :");

        lbMaGV.setText("Mã");

        lbTenGV.setText("Tên");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(lbTenGV, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lbMaGV, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
                .addContainerGap(97, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lbMaGV))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lbTenGV))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Thông tin môn dạy", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 18))); // NOI18N

        tbMonHoc.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Danh Sách Môn Học"
            }
        ));
        tbMonHoc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbMonHocMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbMonHoc);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        tbMonDK.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null},
                {null},
                {null},
                {null}
            },
            new String [] {
                "Danh Sách Môn Đăng Ký"
            }
        ));
        tbMonDK.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbMonDKMouseClicked(evt);
            }
        });
        jScrollPane3.setViewportView(tbMonDK);

        btnDangKy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Yes.png"))); // NOI18N
        btnDangKy.setText("Đăng ký");
        btnDangKy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDangKyActionPerformed(evt);
            }
        });

        btnLamMoi.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Button-Refresh-icon.png"))); // NOI18N
        btnLamMoi.setText("Làm mới");
        btnLamMoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLamMoiActionPerformed(evt);
            }
        });

        btnHuy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Button-Delete-icon (1).png"))); // NOI18N
        btnHuy.setText("Hủy");
        btnHuy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHuyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 64, Short.MAX_VALUE)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 351, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(btnDangKy)
                .addGap(58, 58, 58)
                .addComponent(btnLamMoi)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 302, Short.MAX_VALUE)
                .addComponent(btnHuy, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(82, 82, 82))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDangKy)
                    .addComponent(btnLamMoi)
                    .addComponent(btnHuy)))
        );

        btnThoat.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Button-Close-icon (2).png"))); // NOI18N
        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(btnThoat)
                        .addGap(94, 94, 94))))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(482, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(btnThoat)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnDangKyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDangKyActionPerformed
// TODO add your handling code here:
     try {
            // TODO add your handling code here:
            Dangki();
            LoadMonDangki();
            btnDangKy.setEnabled(false);
        } catch (Exception ex) {
            Logger.getLogger(DialogDangKyMonDay.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_btnDangKyActionPerformed

private void btnLamMoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLamMoiActionPerformed
// TODO add your handling code here:
    try{
            LoadMonhoc();
            LoadMonDangki();
        }
        catch(Exception e){
            e.printStackTrace();
        }
}//GEN-LAST:event_btnLamMoiActionPerformed

private void btnHuyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHuyActionPerformed
// TODO add your handling code here:
    try{
            Huy();
            LoadMonDangki();
        }
        catch(Exception e){
            e.printStackTrace();
        }
}//GEN-LAST:event_btnHuyActionPerformed

private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
// TODO add your handling code here:
      this.setVisible(false);
}//GEN-LAST:event_btnThoatActionPerformed

private void tbMonHocMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbMonHocMouseClicked
// TODO add your handling code here:
     btnDangKy.setEnabled(true);
}//GEN-LAST:event_tbMonHocMouseClicked

private void tbMonDKMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbMonDKMouseClicked
// TODO add your handling code here:
     btnHuy.setEnabled(true);
}//GEN-LAST:event_tbMonDKMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DialogDangKyMonDay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DialogDangKyMonDay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DialogDangKyMonDay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DialogDangKyMonDay.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    DialogDangKyMonDay dialog = new DialogDangKyMonDay(new javax.swing.JFrame(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnDangKy;
    private javax.swing.JButton btnHuy;
    private javax.swing.JButton btnLamMoi;
    private javax.swing.JButton btnThoat;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable2;
    private javax.swing.JLabel lbMaGV;
    private javax.swing.JLabel lbTenGV;
    private javax.swing.JTable tbMonDK;
    private javax.swing.JTable tbMonHoc;
    // End of variables declaration//GEN-END:variables
}
